<?php
namespace Home\Controller;
use Think\Controller;
class BuyController extends Controller
{
	public function index()
	{
		if(!$_SESSION['home_user']['phone']){
			$this->error('您没有登录，请重新登录',U('Index/index'));
		}
		$da = M('category')->field('id,name')->where('pid=0')->select();
		if(I('get.cid/d')&&I('get.mid/d')){
			$da = M('session')->field('lamp_session.id,starttime,endtime,price,num,h.hall')->join('lamp_hall h on lamp_session.hid=h.id')->where('lamp_session.cid='.I('get.cid/d').' and lamp_session.mid='.I('get.mid/d'))->select();
			for ($i = 0; $i < count($da); $i ++) {
				$da[$i]['starttime'] = date('Y-m-d',$da[$i]['starttime']);
				$da[$i]['endtime'] = date('Y-m-d',$da[$i]['endtime']);
				// var_dump($da[$i]['starttime']);exit;
			}
			// var_dump($da);
			//$da[2]['starttime'] = date('Y-m-d',$da[2]['starttime']);
			// var_dump($da['starttime']); exit;
			$this->ajaxReturn($da);
		}
		$id = I ('get.id/d');
		$data = M ('cinema')->select ();
		// var_dump($data);exit;
		$this->assign ('list',$data);
		$this->assign ('id',$id);
		$this->assign ('da',$da);
		$this->display();
	}
	//购买页
	public function buy ()
	{
		//var_dump($_GET);exit;
		if(!$_SESSION['home_user']['phone']){
			$this->error('您没有登录，请重新登录',U('Index/index'));
		}
		$da = M('category')->field('id,name')->where('pid=0')->select();
		$id = I ('get.id/d');
		$data = M ('session')->field ('id,mid,cid,hid,starttime,endtime,price,num')->find ($id);
		$movie = M ('movie')->field ('name')->find ($data['mid']);
		$hall = M ('hall')->field ('hall')->find ($data['hid']);
		$cinema = M ('cinema')->field ('name')->find ($data['cid']);
		$icon = M ('img')->field ('icon')->where ('face=1 and mid='.$data['mid'])->find();

		//积分抵扣操作
		$dikou = M('score')->field('score')->where('uid='.$_SESSION['home_user']['id'])->select();
		// var_dump($dikou);exit;
		$this->assign ('dikou',$dikou['0']);

		//var_dump($data);exit;
		$this->assign ('icon',$icon);
		$this->assign ('movie',$movie);
		$this->assign ('cinema',$cinema);
		$this->assign ('hall',$hall);
		$this->assign ('data',$data);
		$this->assign ('da',$da);
		$this->display ();
	}
	
	public function succ ()
	{
		/*var_dump($_POST);
		 exit;*/
		 //积分抵钱操作
		 $dikou =  I('post.dikou');
		 $score = M('score');
		 $list = $score->field('score')->where('uid='.$_SESSION['home_user']['id'])->select();
		 //积分兑换操作
		 if($list['0']['score']/10>=$_POST['amount']) {
		 	// var_dump($_POST['amount']);
		 	// var_dump('dsa');exit;

		 	$uuid = uuid();
		 	$info['orderNumber'] = $uuid;
		 	$info['sid'] = I('get.sid/d');
		 	$info['uid'] = $_SESSION['home_user']['id'];
		 	$info['amount'] = $_POST['amount'];
		 	$info['status'] = 1;
		 	$info['num'] = $_POST['num'];
		 	$info['time'] = time();
		 	$orders = D('orders');

		 	$jifen = $info['amount'];
		 	//积分兑换成功 后台的价格
 			$info['amount'] = 0;


		 	if(!$orders->create($info)) {
		 		$this->error ($order->getError());
		 	} else {
		 			
		 	
		 		if ($orders->add()) {
		 			$score->where('uid='.$_SESSION['home_user']['id'])->setDec('score',$jifen);

	 				$this->success('积分兑换成功',U('person/index'));
		 			
		 		 } else {
		 			$this->error('积分兑换失败');
		 		 }
		 	} 

		 } else {
		 			// var_dump('3');exit;
		 		 if($dikou>$list['0']['score']) {
		 		 	$this->error('你输入的积分大于你拥有的积分,请重新输入');
		 		 	exit;
		 		 } 
		 		 $book = ceil($dikou/10);

		 	       

		 		$uuid = uuid();
		 		$info['orderNumber'] = $uuid;
		 		$info['sid'] = I('get.sid/d');
		 		$info['uid'] = $_SESSION['home_user']['id'];
		 		$info['amount'] = $_POST['amount']-$book;//抵扣后生成的积分
		 		$info['status'] = 1;
		 		$info['num'] = $_POST['num'];
		 		$info['time'] = time();

		 		//生成积分操作
		 		$jifen = $info['amount']-$dikou;
		 		// $jifen['uid'] = $_SESSION['home_user']['id'];
		 		// var_dump($jifen);exit;

		 		//var_dump($info); exit;
		 		$orders = D('orders');
		 		if(!$orders->create($info)) {
		 			$this->error ($order->getError());
		 		} else {
		 		
		 			if ($orders->add()) {
		 				//积分加入数据库
		 			
		 				if(empty($list['0']['score'])) 
		 				{	
		 					// var_dump($jifen);exit;
		 					$score = M('score');
		 					$mm['score'] = $info['amount'];
		 					$mm['uid'] = $_SESSION['home_user']['id'];
		 					 $data = $score->add($mm);
		 					 // var_dump($data);exit;
		 					$this->success('购买成功',U('person/index'));

		 					 // exit;
		 				} else {
		 					// var_dump('21');exit;
		 					 $score->where('uid='.$_SESSION['home_user']['id'])->setInc('score',$jifen);

		 					$this->success('购买成功',U('person/index'));
		 				}
		 				// M('score')->where('uid='.$_SESSION['home_user']['id'])->save($jifen);
		 			 } else {
		 				$this->error('购买失败');
		 			 }
		 		} 
		 	}

		 	
		 }


		
	
		


	
}